using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Common;
using RMAS.DAL;

namespace RMAS.DAL
{
    /// <summary>
    /// Summary description for QT_PROMOTION.
     /// </summary>
	public class QT_PROMOTIONDao
    {
	    private string _SQLSelectByKey;
	    private string _SQLUpdate;
	    private string _SQLInsert;
	    private string _SQLDelete;
	    private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion
        
        #region Constructor

        public QT_PROMOTIONDao()
        {
            _dbConnection = null;
		    InitSqlQuery();
        }

        public QT_PROMOTIONDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
       {
            string query;

            query = @"SELECT ID, Name, DiscountMoney, DiscountPercent, DateBegin, DateEnd, TypeDiscount, DateCreate, UserName, IsUse, IsEnd, Note, IDPOS FROM QT_PROMOTION WHERE (ID = @ID)";
	        _SQLSelectByKey = query;

            query = @"UPDATE QT_PROMOTION SET Name = @Name, DiscountMoney = @DiscountMoney, DiscountPercent = @DiscountPercent, DateBegin = @DateBegin, DateEnd = @DateEnd, TypeDiscount = @TypeDiscount, DateCreate = @DateCreate, UserName = @UserName, IsUse = @IsUse, IsEnd = @IsEnd, Note = @Note, IDPOS = @IDPOS WHERE (ID = @ID)";
	        _SQLUpdate = query;

            query = @"INSERT INTO QT_PROMOTION VALUES (@ID, @Name, @DiscountMoney, @DiscountPercent, @DateBegin, @DateEnd, @TypeDiscount, @DateCreate, @UserName, @IsUse, @IsEnd, @Note, @IDPOS)";
	        _SQLInsert = query;

            query = @"DELETE FROM QT_PROMOTION WHERE (ID = @ID)";
	        _SQLDelete = query;

            query = @"SELECT ID, Name, DiscountMoney, DiscountPercent, DateBegin, DateEnd, TypeDiscount, DateCreate, UserName, IsUse, IsEnd, Note, IDPOS FROM QT_PROMOTION";
	        _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

				if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(QT_PROMOTION obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                         DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.String);
                         DalTools.addDbDataParameter(command, "Name", obj.Name, DbType.String);
         DalTools.addDbDataParameter(command, "DiscountMoney", obj.DiscountMoney, DbType.Decimal);
         DalTools.addDbDataParameter(command, "DiscountPercent", obj.DiscountPercent, DbType.Double);
         DalTools.addDbDataParameter(command, "DateBegin", obj.DateBegin, DbType.DateTime);
         DalTools.addDbDataParameter(command, "DateEnd", obj.DateEnd, DbType.DateTime);
         DalTools.addDbDataParameter(command, "TypeDiscount", obj.TypeDiscount, DbType.String);
         DalTools.addDbDataParameter(command, "DateCreate", obj.DateCreate, DbType.DateTime);
         DalTools.addDbDataParameter(command, "UserName", obj.UserName, DbType.String);
         DalTools.addDbDataParameter(command, "IsUse", obj.IsUse, DbType.Boolean);
         DalTools.addDbDataParameter(command, "IsEnd", obj.IsEnd, DbType.Boolean);
         DalTools.addDbDataParameter(command, "Note", obj.Note, DbType.String);
         DalTools.addDbDataParameter(command, "IDPOS", obj.IDPOS, DbType.String);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(QT_PROMOTION obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                         DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.String);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(QT_PROMOTION obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(QT_PROMOTION obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties
     
        private QT_PROMOTIONList SelectListByCommand(IDbCommand command)
        {
            QT_PROMOTIONList list = new QT_PROMOTIONList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
		QT_PROMOTION obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new QT_PROMOTION();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
		
        private QT_PROMOTIONList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            QT_PROMOTIONList list = new QT_PROMOTIONList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				QT_PROMOTION obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
				command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new QT_PROMOTION();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	
        private void ReadProperties(QT_PROMOTION obj, IDataReader reader)
        {
            try
            {
                                if(!(reader["ID"] is DBNull))
		{
			obj.ID = (string)reader["ID"];
		}
                if(!(reader["Name"] is DBNull))
		{
			obj.Name = (string)reader["Name"];
		}
                if(!(reader["DiscountMoney"] is DBNull))
		{
			obj.DiscountMoney = (decimal)reader["DiscountMoney"];
		}
                if(!(reader["DiscountPercent"] is DBNull))
		{
			obj.DiscountPercent = (double)reader["DiscountPercent"];
		}
                if(!(reader["DateBegin"] is DBNull))
		{
			obj.DateBegin = (DateTime)reader["DateBegin"];
		}
                if(!(reader["DateEnd"] is DBNull))
		{
			obj.DateEnd = (DateTime)reader["DateEnd"];
		}
                if(!(reader["TypeDiscount"] is DBNull))
		{
			obj.TypeDiscount = (string)reader["TypeDiscount"];
		}
                if(!(reader["DateCreate"] is DBNull))
		{
			obj.DateCreate = (DateTime)reader["DateCreate"];
		}
                if(!(reader["UserName"] is DBNull))
		{
			obj.UserName = (string)reader["UserName"];
		}
                if(!(reader["IsUse"] is DBNull))
		{
			obj.IsUse = (bool)reader["IsUse"];
		}
                if(!(reader["IsEnd"] is DBNull))
		{
			obj.IsEnd = (bool)reader["IsEnd"];
		}
                if(!(reader["Note"] is DBNull))
		{
			obj.Note = (string)reader["Note"];
		}
                if(!(reader["IDPOS"] is DBNull))
		{
			obj.IDPOS = (string)reader["IDPOS"];
		}
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion
      
        public QT_PROMOTION SelectByKey(string ID)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "ID", ID, DbType.String);
      	    QT_PROMOTIONList list =  SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public QT_PROMOTION SelectByKey(string ID, IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "ID", ID, DbType.String);
      	    QT_PROMOTIONList list =  SelectListByCommand(command, trans);
			
            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }
		
        public QT_PROMOTIONList SelectAll()
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectAll;
      	    QT_PROMOTIONList list =  SelectListByCommand(command);
	        return list;
        }
	public QT_PROMOTIONList SelectAll(IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            if (!StaticVar.IsServer)
            {
                _SQLSelectAll = _SQLSelectAll + " WHERE IDPOS = @IDPOS";
                DalTools.addDbDataParameter(command, "IDPOS", StaticVar.CPUID, DbType.String);
            }
            command.CommandText = _SQLSelectAll;
      	    QT_PROMOTIONList list =  SelectListByCommand(command, trans);
	        return list;
        }
         public QT_PROMOTIONList SelectByCommand(string Command)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            if (!StaticVar.IsServer)
            {
                _SQLSelectAll = _SQLSelectAll + " WHERE IDPOS = @IDPOS";
                DalTools.addDbDataParameter(command, "IDPOS", StaticVar.CPUID, DbType.String);
            }
            command.CommandText = Command;
      	    QT_PROMOTIONList list =  SelectListByCommand(command);
	        return list;
        }

        public QT_PROMOTIONList SelectByCondition(QT_PROMOTION obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM QT_PROMOTION " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            QT_PROMOTIONList list = SelectListByCommand(command, trans);
            return list;
        }
		
		 public QT_PROMOTIONList SelectByCondition(QT_PROMOTION obj)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM QT_PROMOTION " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            QT_PROMOTIONList list = SelectListByCommand(command);
            return list;
        }
        private void DeleteByCondition(QT_PROMOTION obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM QT_PROMOTION " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(QT_PROMOTION obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            	if (obj.IsSetID)
	{
    	    s += " AND ID = @ID ";
	}
	if (obj.IsSetName)
	{
    	    s += " AND Name = @Name ";
	}
	if (obj.IsSetDiscountMoney)
	{
    	    s += " AND DiscountMoney = @DiscountMoney ";
	}
	if (obj.IsSetDiscountPercent)
	{
    	    s += " AND DiscountPercent = @DiscountPercent ";
	}
	if (obj.IsSetDateBegin)
	{
    	    s += " AND DateBegin = @DateBegin ";
	}
	if (obj.IsSetDateEnd)
	{
    	    s += " AND DateEnd = @DateEnd ";
	}
	if (obj.IsSetTypeDiscount)
	{
    	    s += " AND TypeDiscount = @TypeDiscount ";
	}
	if (obj.IsSetDateCreate)
	{
    	    s += " AND DateCreate = @DateCreate ";
	}
	if (obj.IsSetUserName)
	{
    	    s += " AND UserName = @UserName ";
	}
	if (obj.IsSetIsUse)
	{
    	    s += " AND IsUse = @IsUse ";
	}
	if (obj.IsSetIsEnd)
	{
    	    s += " AND IsEnd = @IsEnd ";
	}
	if (obj.IsSetNote)
	{
    	    s += " AND Note = @Note ";
	}
	if (obj.IsSetIDPOS)
	{
    	    s += " AND IDPOS = @IDPOS ";
	}
            if (s.Length > 0)
            {
                s =" WHERE "+s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, QT_PROMOTION obj)
        {
	    if (obj == null) { return;}
            	            if (obj.IsSetID)
            {
            	DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.String); 
            }
            if (obj.IsSetName)
            {
            	DalTools.addDbDataParameter(command, "Name", obj.Name, DbType.String); 
            }
            if (obj.IsSetDiscountMoney)
            {
            	DalTools.addDbDataParameter(command, "DiscountMoney", obj.DiscountMoney, DbType.Decimal); 
            }
            if (obj.IsSetDiscountPercent)
            {
            	DalTools.addDbDataParameter(command, "DiscountPercent", obj.DiscountPercent, DbType.Double); 
            }
            if (obj.IsSetDateBegin)
            {
            	DalTools.addDbDataParameter(command, "DateBegin", obj.DateBegin, DbType.DateTime); 
            }
            if (obj.IsSetDateEnd)
            {
            	DalTools.addDbDataParameter(command, "DateEnd", obj.DateEnd, DbType.DateTime); 
            }
            if (obj.IsSetTypeDiscount)
            {
            	DalTools.addDbDataParameter(command, "TypeDiscount", obj.TypeDiscount, DbType.String); 
            }
            if (obj.IsSetDateCreate)
            {
            	DalTools.addDbDataParameter(command, "DateCreate", obj.DateCreate, DbType.DateTime); 
            }
            if (obj.IsSetUserName)
            {
            	DalTools.addDbDataParameter(command, "UserName", obj.UserName, DbType.String); 
            }
            if (obj.IsSetIsUse)
            {
            	DalTools.addDbDataParameter(command, "IsUse", obj.IsUse, DbType.Boolean); 
            }
            if (obj.IsSetIsEnd)
            {
            	DalTools.addDbDataParameter(command, "IsEnd", obj.IsEnd, DbType.Boolean); 
            }
            if (obj.IsSetNote)
            {
            	DalTools.addDbDataParameter(command, "Note", obj.Note, DbType.String); 
            }
            if (obj.IsSetIDPOS)
            {
            	DalTools.addDbDataParameter(command, "IDPOS", obj.IDPOS, DbType.String); 
            }
        }
        #endregion
    }
}